package com.lbn.monitor.widget.calendar;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;


import com.lbn.monitor.R;

import java.util.Calendar;
import java.util.List;

/**
 * Created by Administrator on 2016/9/13.
 */
public class CalendarView extends FrameLayout implements ViewPager.OnPageChangeListener, View.OnClickListener {

    private ViewPager viewPager;

    private CalendarFragmentAdapter adapter;

    private TextView textYear;

    private TextView textMonth;

    private TextView title;

    //    private static CalendarObserver mObserver;
    private CalendarListener listener;

    private int year, month;

//    private Button monthLeft,monthRight,yearLeft,yearRight;


    public CalendarView(Context context) {
        this(context, null);
    }

    public CalendarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CalendarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setTitle(String title) {
        this.title.setText(title);
    }

    public void init() {

//        mObserver = new CalendarObserver();

        addView(View.inflate(getContext(), R.layout.layout_calendar, null));

        textYear = (TextView) findViewById(R.id.year);
        textMonth = (TextView) findViewById(R.id.month);
        title = (TextView) findViewById(R.id.title);

        findViewById(R.id.month_right).setOnClickListener(this);
        findViewById(R.id.month_left).setOnClickListener(this);
        findViewById(R.id.year_right).setOnClickListener(this);
        findViewById(R.id.year_left).setOnClickListener(this);
        findViewById(R.id.confirm).setOnClickListener(this);
        findViewById(R.id.cancel).setOnClickListener(this);

        viewPager = (ViewPager) findViewById(R.id.view_pager);

        adapter = new CalendarFragmentAdapter(((FragmentActivity) getContext()).getSupportFragmentManager());

        viewPager.setAdapter(adapter);

        Calendar calendar = Calendar.getInstance();

        textYear.setText(String.valueOf(calendar.get(Calendar.YEAR)));
        textMonth.setText(String.valueOf(calendar.get(Calendar.MONTH) + 1)+"月");

        viewPager.setCurrentItem((calendar.get(Calendar.YEAR) - 1) * 12 + calendar.get(Calendar.MONTH) + 1);

        viewPager.addOnPageChangeListener(this);

        CalendarObserver.setViewPager(viewPager);

        setTitle("请选择日期区间");

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        year = position / 12;
        month = position - year * 12;
        if (position % 12 != 0) {
            year = year + 1;
        }
        if (month == 0) {
            month = 12;
        }
        textYear.setText(String.valueOf(year));
        textMonth.setText(String.valueOf(month) + "月");
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

//    public static CalendarObserver getObserver() {
//        return mObserver;
//    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.month_left:
                viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
                break;
            case R.id.month_right:
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
                break;
            case R.id.year_left:
                for (int i = 0; i < 12; i++) {
                    viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
                }
                break;
            case R.id.year_right:
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 12);
                break;
            case R.id.confirm:
                if (listener != null) {
                    List<Pos> list = CalendarObserver.getData();

                    if(list.size()>1){
                        CalendarData data1 = list.get(0).getAdapter().getItem(list.get(0).getPos());
                        CalendarData data2 = list.get(1).getAdapter().getItem(list.get(1).getPos());
                        long startTime = TimeUtils.string2Time(data1.year + "-" + data1.month + "-" + data1.day);
                        long endTime = TimeUtils.string2Time(data2.year + "-" + data2.month + "-" + data2.day);
                        if(startTime>endTime){
                            listener.startTime(endTime);
                            listener.endTime(startTime);
                        }else{
                            listener.startTime(startTime);
                            listener.endTime(endTime);
                        }
                        listener.confirm();
                    }else{
//                        CalendarData data1 = list.get(0).getAdapter().getItem(list.get(0).getPos());
//                        long startTime = TimeUtils.string2Time(data1.year + "-" + data1.month + "-" + data1.day);
//                        listener.startTime(startTime);
//                        listener.endTime(startTime);
                        listener.error("请选择结束日期");
                    }
                }
                break;
            case R.id.cancel:
                if (listener != null) {
                    listener.cancel();
                }
                break;
        }
    }

    public void setCalendarListener(CalendarListener listener) {
        this.listener = listener;
    }

    public interface CalendarListener {

        void startTime(long time);

        void endTime(long time);

        void confirm();

        void cancel();

        void error(String msg);
    }
}
